package com.sj.purchase.modules.cost.web;

import com.sj.purchase.common.Result;
import com.sj.purchase.common.ResultGenerator;
import com.sj.purchase.modules.cost.domain.imei.Inventory;
import com.sj.purchase.modules.cost.repository.imei.InventoryRepository;
import com.sj.purchase.modules.cost.views.OutInventoryVO;
import com.sj.purchase.common.utils.mapper.BeanMapper;
import com.sj.purchase.modules.cost.domain.imei.InventoryId;
import com.sj.purchase.modules.cost.views.InventoryVO;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.sj.purchase.common.ResultGenerator.error;
import static com.sj.purchase.common.ResultGenerator.ok;

/**
 * Created by yangrd on 2017/10/16
 **/
@RestController
@RequestMapping("api/inventory")
public class InventoryController {

    @Autowired
    private InventoryRepository repository;

    private static final String FORMAT = "yyyy-MM-dd";

    @GetMapping("/info")
    public Inventory getInfo(InventoryId inventoryId) {
        return repository.findOne(inventoryId);
    }

    //--出库明细

    @GetMapping("out-stock")
    public Page<OutInventoryVO> getOutStockAll(Date start, Date end, Pageable pageable) {
        Page<Inventory> inventoryPage = repository.findByOutStockDateBetweenOrderByOutStockDate(start, end, pageable);
        return new PageImpl<>(mapOutInventoryVOList(inventoryPage.getContent()), pageable, inventoryPage.getTotalElements());
    }

    @GetMapping("out-stock/excel")
    public void exportOutStockExcel(Date start, Date end) {
        String excelName = String.format("%s--%s %s", DateFormatUtils.format(start, FORMAT), DateFormatUtils.format(end, FORMAT), "出库明细");
        ExcelDownloadUtil.easyPoi(excelName, OutInventoryVO.class, mapOutInventoryVOList(repository.findByOutStockDateBetweenOrderByOutStockDate(start, end, null).getContent()));
    }

    //--在库明细

    @GetMapping
    public Page<InventoryVO> getAll(Date start, Date end, Pageable pageable) {
        Page<Inventory> inventoryPage = repository.findById_InStockDateBetweenAndOutOrderNoIsNullOrderById_InStockDateDesc(start, end, pageable);
        return new PageImpl<>(mapInventoryVOList(inventoryPage.getContent()), pageable, inventoryPage.getTotalElements());
    }

    @GetMapping("excel")
    public void exportExcel(Date start, Date end) {
        String excelName = String.format("%s--%s %s", DateFormatUtils.format(start, FORMAT), DateFormatUtils.format(end, FORMAT), "在库明细");
        ExcelDownloadUtil.easyPoi(excelName, InventoryVO.class, mapInventoryVOList(repository.findById_InStockDateBetweenAndOutOrderNoIsNullOrderById_InStockDateDesc(start, end, null).getContent()));
    }

    @DeleteMapping
    public Result<String> delete(String imei, Date inStockDate) {
        InventoryId inventoryId = new InventoryId(imei, inStockDate);
        Inventory inventory = repository.findOne(inventoryId);
        if (Objects.isNull(inventory)) {
            return ResultGenerator.error();
        } else {
            repository.delete(inventoryId);
            return ResultGenerator.ok();
        }
    }

    private List<OutInventoryVO> mapOutInventoryVOList(List<Inventory> inventories) {
        return inventories.stream().map(this::mapOutInventory).collect(Collectors.toList());
    }

    private List<InventoryVO> mapInventoryVOList(List<Inventory> inventories) {
        return inventories.stream().map(this::mapInventory).collect(Collectors.toList());
    }

    private OutInventoryVO mapOutInventory(Inventory inventory) {
        return BeanMapper.map(inventory, OutInventoryVO.class).setImei(inventory.getId().getImei()).setInStockDate(inventory.getId().getInStockDate());
    }

    private InventoryVO mapInventory(Inventory inventory) {
        return BeanMapper.map(inventory, InventoryVO.class).setImei(inventory.getId().getImei()).setInStockDate(inventory.getId().getInStockDate());
    }
}
